import Slide from '@/models/slide';
import SmsService from '@/services/sms';
import { action, observable } from 'mobx';

/**
 * 生成滑块验证码Store
 */
export default class SlideStore {
  @observable
  slideInfo?: Slide;
  @observable
  loading: boolean = false;

  @action
  async getSliderCaptcha() {
    this.loading = true;
    this.slideInfo = (await SmsService.getSliderCaptcha()).dataInfo;
    this.loading = false;
  }

  /**
   * 发送验证码
   * @param mobile
   * @param sendType 验证码类型 1-手机号注册 2-修改密码获取验证码
   * @param percentage 滑块距离
   * @returns
   */
  @action
  async sendSms(
    mobile: string,
    sendType: number,
    percentage: number,
  ): Promise<boolean> {
    if (!this.slideInfo) {
      return false;
    }
    this.loading = true;
    const result = await SmsService.sendSms(
      mobile,
      sendType,
      this.slideInfo?.id ?? '',
      percentage,
    );
    this.loading = false;
    return result.isSuccess;
  }
}
